package system

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
	decimal "github.com/flipped-aurora/gin-vue-admin/server/service/system"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type Seckill_activityApi struct{}

// CreateSeckill_activity 创建秒杀活动
// @Tags Seckill_activity
// @Summary 创建秒杀活动
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.Seckill_activity true "创建秒杀活动"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /seckill_activity/createSeckill_activity [post]
func (seckill_activityApi *Seckill_activityApi) CreateSeckill_activity(c *gin.Context) {
	ctx := c.Request.Context()
	var req decimal.CreateSeckillActivityRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := seckill_activityService.CreateSeckill_activity(ctx, &req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteSeckill_activity 删除秒杀活动
// @Tags Seckill_activity
// @Summary 删除秒杀活动
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.Seckill_activity true "删除秒杀活动"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /seckill_activity/deleteSeckill_activity [delete]
func (seckill_activityApi *Seckill_activityApi) DeleteSeckill_activity(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ID := c.Query("ID")
	err := seckill_activityService.DeleteSeckill_activity(ctx, ID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteSeckill_activityByIds 批量删除秒杀活动
// @Tags Seckill_activity
// @Summary 批量删除秒杀活动
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /seckill_activity/deleteSeckill_activityByIds [delete]
func (seckill_activityApi *Seckill_activityApi) DeleteSeckill_activityByIds(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	IDs := c.QueryArray("IDs[]")
	err := seckill_activityService.DeleteSeckill_activityByIds(ctx, IDs)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateSeckill_activity 更新秒杀活动
// @Tags Seckill_activity
// @Summary 更新秒杀活动
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.Seckill_activity true "更新秒杀活动"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /seckill_activity/updateSeckill_activity [put]
func (seckill_activityApi *Seckill_activityApi) UpdateSeckill_activity(c *gin.Context) {
	// 从ctx获取标准context进行业务行为
	ctx := c.Request.Context()

	var seckill_activity system.Seckill_activity
	err := c.ShouldBindJSON(&seckill_activity)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = seckill_activityService.UpdateSeckill_activity(ctx, seckill_activity)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindSeckill_activity 用id查询秒杀活动
// @Tags Seckill_activity
// @Summary 用id查询秒杀活动
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "用id查询秒杀活动"
// @Success 200 {object} response.Response{data=system.Seckill_activity,msg=string} "查询成功"
// @Router /seckill_activity/findSeckill_activity [get]
func (seckill_activityApi *Seckill_activityApi) FindSeckill_activity(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ID := c.Query("ID")
	reseckill_activity, err := seckill_activityService.GetSeckill_activity(ctx, ID)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(reseckill_activity, c)
}

// GetSeckill_activityList 分页获取秒杀活动列表
// @Tags Seckill_activity
// @Summary 分页获取秒杀活动列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query systemReq.Seckill_activitySearch true "分页获取秒杀活动列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /seckill_activity/getSeckill_activityList [get]
func (seckill_activityApi *Seckill_activityApi) GetSeckill_activityList(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var pageInfo systemReq.Seckill_activitySearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := seckill_activityService.GetSeckill_activityInfoList(ctx, pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetSeckill_activityPublic 不需要鉴权的秒杀活动接口
// @Tags Seckill_activity
// @Summary 不需要鉴权的秒杀活动接口
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /seckill_activity/getSeckill_activityPublic [get]
func (seckill_activityApi *Seckill_activityApi) GetSeckill_activityPublic(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	seckill_activityService.GetSeckill_activityPublic(ctx)
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的秒杀活动接口信息",
	}, "获取成功", c)
}
